import validate from 'wechat-validate'
Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    attachment: [],//图片
    // 房屋列表
    houseList: [],
    houseId: '',
    houseInfo: '',
    // 维修项目
    repairItem: [],
    repairItemId: '',
    repairItemName: '',
    //预约日期
    appointment: '',
    description:'',
    mobile:''
  },
  rules: {
    houseId: [
      {required: true, message: '请选择报修房屋!'}
    ],
    repairItemId: [
      {required: true, message: '请选择维修的项目!'}
    ],
    mobile: [
      {required: true, message: '请填写手机号码!'},
      {pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!'},
    ],
    appointment: [
      {required: true, message: '请选择预约日期!'}
    ],
    description: [
      {required: true, message: '请填写问题描述!'}
    ],
  },
  behaviors: [validate],
  onLoad({id}) {
    if(id){
      this.getRepairInfo(id)
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      });
    }
    this.getHouseList()
    this.getRepairItem()
  },
  // 修改报修信息
  async getRepairInfo(id){
    const res=await wx.http.get('/repair/'+id)
    this.setData({...res})
  },
  // 查询我的房屋列表
  async getHouseList() {
    let houseList = await wx.http.get('/room')
    // console.log(houseList)
    houseList = houseList.map(item => {
      return {
        name: item.point + item.building + item.room,
        id: item.id
      }
    })
    this.setData({
      houseList
    })
  },
  // 选择报修房屋
  selectHouse({detail}) {
    // console.log(detail);
    this.setData({
      houseId: detail.id,
      houseInfo: detail.name
    })
  },
  // 查询报修项目列表
  async getRepairItem() {
    const repairItem = await wx.http.get('/repairItem')
    // console.log(repairItem)
    this.setData({
      repairItem
    })
  },
  // 选择报修项目
  selectRepairItem({
    detail
  }) {
    //  console.log(detail)
    this.setData({
      repairItemId: detail.id,
      repairItemName: detail.name
    })
  },
  // 报修房屋
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  // 维修项目
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  // 选择报修时间
  selectDateLayer(event) {
    // console.log(event.detail)
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.dataFormat(event.detail)
    })
  },
  // 上传图片
  uploadImg(event){
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: wx.http.baseURL+'/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header:{
        Authorization:`Bearer ${getApp().token}`
      },
      success:(res)=> {
        // 上传完成需要更新 fileList
        const {code,message,data}=JSON.parse(res.data)
        if(code!==10000) return wx.utils.toast(message)
        this.setData({
          attachment:[...this.data.attachment,{url:data.url}]
        })
      },
      fail:err=>{
        console.log(err)
      }
    });
  },
  // 删除图片
  deleteImg(event){
    // console.log(event)
    this.setData({
      attachment:this.data.attachment.filter((item,index)=>{
        return index!==event.detail.index
      })
    })
  },
  // 时间选择器
  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  // 提交报修项目
  async goList() {
    const flag=this.validate()
    if(!flag) return
    const {id,houseId,repairItemId,mobile,appointment,description,attachment}=this.data
    await wx.http.post('/repair',{
      id,houseId,repairItemId,mobile,appointment,description,attachment
    })
    wx.utils.toast('提交成功')
    setTimeout(()=>{
      wx.reLaunch({
        url: '/repair_pkg/pages/list/index',
      })
    },500)
  },
})